package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "地址簿接口")
public class AdressBookController {
    @Resource
    private IAddressBookService addressBookService;

    /**
     * 新增地址簿
     *
     * @param dto
     */
    @PostMapping
    @ApiOperation("新增地址簿")
    public void add(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.add(dto);
    }

    /**
     * 地址簿分页查询接口
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("地址簿分页查询接口")
    public PageResult<AddressBook> page(AddressBookPageQueryReqDTO dto) {
        return addressBookService.page(dto);
    }

    /**
     * 地址薄详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("地址薄详情")
    public AddressBook findById(@PathVariable Long id) {
        return addressBookService.findById(id);
    }

    /**
     * 地址薄修改
     *
     * @param id
     * @param dto
     */
    @PutMapping("/{id}")
    @ApiOperation("地址薄修改")
    public void update(@PathVariable Long id, @RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.update(id,dto);
    }

    /**
     * 设置/取消默认地址
     *
     * @param id
     * @param flag
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void updateDefault(@RequestParam("id") Long id,
                              @RequestParam("flag") Integer flag) {
        addressBookService.updateDefault(id,flag);
    }

    /**
     * 地址普批量删除
     *
     * @param ids
     */
    @DeleteMapping("/batch")
    @ApiOperation("地址簿批量删除")
    public void delete(@RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
    }

    /**
     * 获取默认地址
     *
     * @return
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBook defaultAddress() {
        return addressBookService.defaultAddress();
    }


}
